package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.AppMingxiMeilizhiDao;
import com.vf.admin.client.dao.AppSetupDao;
import com.vf.admin.client.dao.AppUserDao;
import com.vf.admin.client.dto.AppRankingDto;
import com.vf.admin.client.service.AppMingxiMeilizhiService;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingxiMeilizhi;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.AgeUtils;
import com.vf.admin.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.vf.admin.common.Constants.guizuCacheMap;

@Service
public class AppMingxiMeilizhiServiceImpl implements AppMingxiMeilizhiService {
    @Autowired
    private AppMingxiMeilizhiDao appMingxiMeilizhiDao;
    @Autowired
    private AppUserDao appUserDao;
    @Autowired
    private AppSetupDao appSetupDao;

    @Override
    public List<AppMingxiMeilizhi> findList(Integer userid, SplitPageBean splitPage) {
        return appMingxiMeilizhiDao.findList(userid, splitPage);
    }

    @Override
    public AppMingxiMeilizhi get(Integer id) {
        return appMingxiMeilizhiDao.get(id);
    }

    @Override
    public void save(AppMingxiMeilizhi haoYou) {
        appMingxiMeilizhiDao.save(haoYou);
    }
    @Override
    public void addMingxi(Integer userid,Integer num,Integer leixing,String beizhu) {
        AppUser user=appUserDao.get(userid);
        AppMingxiMeilizhi meilizhi=new AppMingxiMeilizhi();
        meilizhi.setUserid(user.getId());
        meilizhi.setPic(user.getPic());
        meilizhi.setPhone(user.getLoginname());
        meilizhi.setNick(user.getNick());
        meilizhi.setLeixing(leixing);
        meilizhi.setNum(num);
        meilizhi.setCreatetime(new Date());
        meilizhi.setBeizhu(beizhu);
        appMingxiMeilizhiDao.save(meilizhi);
    }

    @Override
    public List<AppRankingDto> getRanking(Integer secondType, Map<String, Object> params) {
        List<AppMingxiMeilizhi> appMingxiMeilizhis = new ArrayList<>();
        ArrayList<AppRankingDto> appRankingDtos = new ArrayList<>();
        switch (secondType){
            case 1:
                appMingxiMeilizhis = appMingxiMeilizhiDao.getDayRanking(params);
                break;
            case 2:
                appMingxiMeilizhis = appMingxiMeilizhiDao.getWeekRanking(params);
                break;
            case 3:
                appMingxiMeilizhis = appMingxiMeilizhiDao.getMonthRanking(params);
                break;
            default:
                return null;
        }
        if(appMingxiMeilizhis!=null && appMingxiMeilizhis.size()>0){
            appMingxiMeilizhis.forEach(e->{
                AppRankingDto appRankingDto = new AppRankingDto();
                AppUser appUser = appUserDao.get(e.getUserid());
                if(appUser != null) {
                    appRankingDto.setPic(appUser.getPic());
                    appRankingDto.setNick(appUser.getNick());
                    appRankingDto.setUserId(appUser.getId());
                    appRankingDto.setTotalMeiLi(e.getTotalMeiLi());
                    appRankingDto.setSex(appUser.getSex());
                    appRankingDto.setMySign(appUser.getMysign());
                    appRankingDto.setVoiceSignature(appUser.getVoiceSignature());
                    appRankingDto.setYearOld(AgeUtils.getAgeByBirth(appUser.getBirthday()));
                    appRankingDto.setVoiceSignatureSeconds(appUser.getVoiceSignatureSeconds());
                    //判断用户是否是贵族
                    if (appUser.getIsguizu() == 1) {
                        //是贵族
                        //根据贵族id  获取贵族图标
                        String medal = guizuCacheMap.get(appUser.getNobleid());
                        appRankingDto.setMedal(medal);
                    } else {
                        appRankingDto.setMedal("");
                    }
                    appRankingDto.setNobleid(appUser.getNobleid());
                    appRankingDto.setIsguizu(appUser.getIsguizu());
                    if(appUser.getVipEndDate() != null) {
                        appRankingDto.setIsVip(TimeUtils.afterNow(appUser.getVipEndDate())? 1: 0);
                    }
                }

                appRankingDtos.add(appRankingDto);
            });
            return appRankingDtos;
        }

        return null;
    }


}
